package com.action;

import com.dao.MailDao;
import com.dao.MailReceiveDao;
import com.model.MailReceiver;
import com.model.MailSender;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 管理员信息业务处理类
 */
public class MailReceiveAction extends ActionSupport {

    public static int PAGE_ITEM = 10; // 每页的记录数

    private boolean exist = true;

    @Autowired
    private MailReceiveDao mailReceiveDTao;

    /**
     * 发送邮箱
     */
    private String sender;

    /**
     * 接收邮箱
     */
    private String receiver;

    /**
     * 邮件标题
     */
    private String subject;

    /**
     * 邮件内容
     */
    private String content;


    /**
     * 邮件列表
     *
     * @return
     */
    public String list() {
        HttpServletRequest req = ServletActionContext.getRequest();
        int offset = 0; // 记录偏移量
        int counts = 0; // 总记录数
        try {
            offset = Integer.parseInt(req.getParameter("pager.offset") == null ? "0" : req.getParameter("pager.offset"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        MailReceiver mailReceiver = new MailReceiver();
        mailReceiver.setSubject(subject);
        // 获取总记录数
        counts = mailReceiveDTao.getCount(mailReceiver);
        // 查询本页数据
        List<MailSender> list = mailReceiveDTao.findAll(mailReceiver, offset, PAGE_ITEM);
        // 分页需要的参数
        req.setAttribute("itemSize", counts);
        int page_count = counts % PAGE_ITEM == 0 ? counts / PAGE_ITEM : counts / PAGE_ITEM + 1;
        req.setAttribute("pageItem", PAGE_ITEM);
        req.setAttribute("pageTotal", page_count);
        // 将本页数据放到request中到前台界面显示
        req.setAttribute("list", list);
        return "list";
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}